const express = require('express')
const route = express.Router()
//引入token的解码
const {
    jwtDecode
  } = require("../utils/jwt")
//引入校验中间件
const isLoginMiddle = require("../middleware/isLoginMiddle")
//引入数据库
const adminModel = require("../db/model/adminModel")
const { set } = require('mongoose')
route.get('/logout',isLoginMiddle,async (req,res)=>{
    //拿到token
    const {
        token
      } = req.headers
      //解码token 得到用户名
      const{
        adminName
      }=jwtDecode(token, "666")
    try {
        //然后去数据库修改状态
         await adminModel.updateOne({
            adminName
        }, {
            $set: {
                status: 0
            }
        })
        res.send({
            code: 10000,
            msg: "退出登录成功",
            data: null
        })
    } catch (e) {
        console.log("退出登录出现异常" + e.message);
        res.send({
          code: 10001,
          msg: "退出登录出现异常" + e.message,
          data: null
        })
    }
})
module.exports = route